package de.ifi.lmu.snitzelhunt.util;

import de.ifi.lmu.snitzelhunt.R;
import de.ifi.lmu.snitzelhunt.basics.Route;
import de.ifi.lmu.snitzelhunt.basics.User;
import de.ifi.lmu.snitzelhunt.util.basics.Ranking;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncDatabase_Route_CreateResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncDatabase_Route_DeleteResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncDatabase_Route_ReadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncDatabase_Route_UpdateResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Ranking_CreateResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Ranking_ReadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_RouteInfo_ReadforCreatorResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Route_DeleteResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Route_ReadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_RouteInfo_ReadforUserResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Route_CreateResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Route_UpdateResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Ranking_CreateResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Ranking_ReadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_RouteInfo_ReadforCreatorResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_RouteInfo_ReadforUserResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Route_CreateResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Route_DeleteResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Route_ReadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Route_UpdateResponse;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

/**
 * Simple activity for testing the operations on the local database and on the
 * Server
 * 
 * @author jaschkowitz
 * 
 */
public class TestActivity extends Activity implements
		AsyncServer_Route_DeleteResponse, AsyncServer_Route_UpdateResponse,
		AsyncDatabase_Route_CreateResponse, AsyncDatabase_Route_ReadResponse,
		AsyncDatabase_Route_UpdateResponse, AsyncServer_Route_ReadResponse,
		AsyncServer_RouteInfo_ReadforUserResponse,
		AsyncServer_Route_CreateResponse, AsyncServer_Ranking_CreateResponse,
		AsyncServer_Ranking_ReadResponse,
		AsyncServer_RouteInfo_ReadforCreatorResponse,
		AsyncDatabase_Route_DeleteResponse {
	public Route testR;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		// Comment in the test, use only one of them at a time
		// testLocalDatabase();
		// uploadServerTestRoute();
		// routeInfoUserTest();
		// routeInfoCreatorTest();
		// rankingTest();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.test, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_test, container,
					false);
			return rootView;
		}
	}

	public void uploadServerTestRoute() {
		testR = TestBasics.createDummyroute();
		System.out.println("TestR:" + testR + "\n\n");
		new RouteServerUtility().createRoute(testR, this);

	}

	@Override
	public void processServer_Route_Create(Server_Route_CreateResponse response) {
		Log.i("snitzelhunt util ", "uploaded Route " + response.getHttpStatus()
				+ " " + response.getGlobalID());

		if (response.getHttpStatus() == 200) {
			testR.setGlobal_routeId(response.getGlobalID());
			testR.setCreator("ICH bin anders");

			testR.setName("ICH auch");
			testR.getUsers().get(0).setName("test");
			// new RouteServerUtility().deleteRoute(response.getGlobalID(),
			// this);
			// new RouteServerUtility().updateRoute(testR, this);
			new RouteServerUtility().readRoute(testR.getGlobal_routeId(), this);
		}
	}

	@Override
	public void processServer_Route_Read(Server_Route_ReadResponse response) {
		Log.i("snitzelhunt util ",
				"downloaded Route " + response.getHttpStatus() + " "
						+ response.getRecvRoute());

	}

	@Override
	public void processServer_Route_Update(Server_Route_UpdateResponse response) {
		Log.i("snitzelhunt util ", "updated Route " + response.getHttpStatus());

	}

	@Override
	public void processServer_Route_Delete(Server_Route_DeleteResponse response) {
		Log.i("snitzelhunt util ", "deleted Route " + response.getHttpStatus());

	}

	public void routeInfoUserTest() {
		new RouteServerUtility().readRouteInfo_forUser(new User("test",
				"017564958"), this);

	}

	public void routeInfoCreatorTest() {
		new RouteServerUtility().readRouteInfo_forCreator("abc", this);

	}

	@Override
	public void processServer_RouteInfo_ReadforUser(
			Server_RouteInfo_ReadforUserResponse response) {
		Log.i("snitzelhunt util ",
				"downloaded RouteInfo " + response.getHttpStatus());

		if (response.getHttpStatus() == 200) {
			Log.i("snitzelhunt util ", "RInfo entfangen" + response.getRinfo());
		}
	}

	public void testLocalDatabase() {
		testR = TestBasics.createDummyroute();
		System.out.println("TestR:" + testR + "\n\n");
		DatabaseUtility util = new DatabaseUtility();
		util.createRoute(testR, getBaseContext(), this);
	}

	@Override
	public void processDatabase_Route_Create(long localID) {
		Log.i("snitzelhunt util", "saved Route to " + localID);
		DatabaseUtility util = new DatabaseUtility();
		testR.setLocal_routeID(localID);
		util.readRoute(localID, getBaseContext(), this);
	}

	@Override
	public void processDatabase_Route_Read(Route route) {
		System.out.println(route.toString());

		new DatabaseUtility().deleteRoute(route.getLocal_routeID(),
				getBaseContext(), this);

		// String newCreat = "TestCreator";
		// if (!route.getCreator().equals(newCreat)) {
		// testR = route;
		// testR.setCreator(newCreat);
		// ArrayList<User> users = new ArrayList<User>();
		// users.add(new User("ich bin anders", "123"));
		// users.add(new User("ich auch", "123"));
		// testR.setUsers(users);
		// DatabaseUtility util = new DatabaseUtility();
		// util.updateRoute(testR, getBaseContext(), this);
		// }
	}

	@Override
	public void processDatabase_Route_Delete() {
		Log.i("snitzelhunt util ", "Local Route Delete");

	}

	@Override
	public void processDatabase_Route_Update() {
		DatabaseUtility util = new DatabaseUtility();
		util.readRoute(testR.getLocal_routeID(), getBaseContext(), this);

	}

	public void rankingTest() {
		new RankingServerUtility().createRanking(new Ranking(1,
				"ich bin der Beste", 9000), this);

	}

	@Override
	public void processServer_Ranking_Create(
			Server_Ranking_CreateResponse response) {
		new RankingServerUtility().readRanking(1, this);
	}

	@Override
	public void processServer_Ranking_ReadResponse(
			Server_Ranking_ReadResponse response) {
		Log.i("snitzelhunt util ",
				"downloaded RankingContainer " + response.getHttpStatus());

		if (response.getHttpStatus() == 200) {
			Log.i("snitzelhunt util ",
					"Ranking Container entfangen" + response.getRankCont());

		}
	}

	@Override
	public void processServer_RouteInfo_ReadforCreator(
			Server_RouteInfo_ReadforCreatorResponse response) {
		Log.i("snitzelhunt util ", "downloaded RouteInfo for Creator"
				+ response.getHttpStatus());

		if (response.getHttpStatus() == 200) {
			Log.i("snitzelhunt util ", "RInfo entfangen" + response.getRinfo());

		}
	}

}
